home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / vbdScene.cls < prev    next >
Text File  |  1999-06-19  |  8KB  |  289 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "vbdScene"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ' VbDraw scene object.
  16.  
  17. Implements vbdObject
  18.  
  19. ' The objects in the scene.
  20. Public SceneObjects As Collection
  21.  
  22. ' Drawing properties.
  23. Private m_DrawWidth As Integer
  24. Private m_DrawStyle As DrawStyleConstants
  25. Private m_ForeColor As OLE_COLOR
  26. Private m_FillColor As OLE_COLOR
  27. Private m_FillStyle As FillStyleConstants
  28.  
  29. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As Any) As Long
  30. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
  31.  
  32. ' Move these objects to the beginning of the
  33. ' SceneObjects collection so they are drawn
  34. ' first.
  35. Public Sub MoveToBack(ByVal targets As Collection)
  36. Dim target As vbdObject
  37.  
  38.     ' Remove the objects from SceneObjects.
  39.     RemoveObjects targets
  40.  
  41.     ' Re-add the objects at the beginning.
  42.     For Each target In targets
  43.         If SceneObjects.Count < 1 Then
  44.             SceneObjects.Add target
  45.         Else
  46.             SceneObjects.Add target, , 1
  47.         End If
  48.     Next target
  49. End Sub
  50. ' Move these objects to the end of the
  51. ' SceneObjects collection so they are drawn
  52. ' last.
  53. Public Sub MoveToFront(ByVal targets As Collection)
  54. Dim target As vbdObject
  55.  
  56.     ' Remove the objects from SceneObjects.
  57.     RemoveObjects targets
  58.  
  59.     ' Re-add the objects at the end.
  60.     For Each target In targets
  61.         SceneObjects.Add target
  62.     Next target
  63. End Sub
  64. ' Remove these objects from SceneObjects.
  65. Public Sub RemoveObjects(ByVal targets As Collection)
  66. Dim target As vbdObject
  67. Dim obj As vbdObject
  68. Dim i As Integer
  69.  
  70.     ' Remove the objects from SceneObjects.
  71.     For Each target In targets
  72.         ' Find this target.
  73.         i = 1
  74.         For Each obj In SceneObjects
  75.             If obj Is target Then
  76.                 SceneObjects.Remove i
  77.                 Exit For
  78.             End If
  79.             i = i + 1
  80.         Next obj
  81.     Next target
  82. End Sub
  83.  
  84. ' Add this transformation to the current one.
  85. Private Sub vbdObject_AddTransformation(M() As Single)
  86. Dim obj As vbdObject
  87.  
  88.     For Each obj In SceneObjects
  89.         obj.AddTransformation M
  90.     Next obj
  91. End Sub
  92.  
  93. Private Property Set vbdObject_Canvas(ByVal RHS As PictureBox)
  94.     ' Do nothing. This object is not directly
  95.     ' creatable by the user.
  96. End Property
  97.  
  98. Private Property Get vbdObject_Canvas() As PictureBox
  99.     ' Do nothing. This object is not directly
  100.     ' creatable by the user.
  101.     Set vbdObject_Canvas = Nothing
  102. End Property
  103.  
  104. ' Clear the object's transformation.
  105. Private Sub vbdObject_ClearTransformation()
  106. Dim obj As vbdObject
  107.  
  108.     For Each obj In SceneObjects
  109.         obj.ClearTransformation
  110.     Next obj
  111. End Sub
  112.  
  113. ' Draw the object in a metafile.
  114. Private Sub vbdObject_DrawInMetafile(ByVal mf_dc As Long)
  115. Dim obj As vbdObject
  116.  
  117.     For Each obj In SceneObjects
  118.         obj.DrawInMetafile mf_dc
  119.     Next obj
  120. End Sub
  121. Private Sub Class_Initialize()
  122.     Set SceneObjects = New Collection
  123. End Sub
  124.  
  125.  
  126. ' Return this object's bounds.
  127. Private Sub vbdObject_Bound(xmin As Single, ymin As Single, xmax As Single, ymax As Single)
  128.     BoundObjects SceneObjects, xmin, ymin, xmax, ymax
  129. End Sub
  130.  
  131. ' Return the object's DrawWidth.
  132. Public Property Get vbdObject_DrawWidth() As Integer
  133.     vbdObject_DrawWidth = m_DrawWidth
  134. End Property
  135. ' Set the object's DrawWidth.
  136. Public Property Let vbdObject_DrawWidth(ByVal new_value As Integer)
  137.     m_DrawWidth = new_value
  138. End Property
  139.  
  140. ' Return the object's DrawStyle.
  141. Public Property Get vbdObject_DrawStyle() As DrawStyleConstants
  142.     vbdObject_DrawStyle = m_DrawStyle
  143. End Property
  144. ' Set the object's DrawStyle.
  145. Public Property Let vbdObject_DrawStyle(ByVal new_value As DrawStyleConstants)
  146.     m_DrawStyle = new_value
  147. End Property
  148.  
  149. ' Return the object's ForeColor.
  150. Public Property Get vbdObject_ForeColor() As OLE_COLOR
  151.     vbdObject_ForeColor = m_ForeColor
  152. End Property
  153. ' Set the object's ForeColor.
  154. Public Property Let vbdObject_ForeColor(ByVal new_value As OLE_COLOR)
  155.     m_ForeColor = new_value
  156. End Property
  157.  
  158. ' Return the object's FillColor.
  159. Public Property Get vbdObject_FillColor() As OLE_COLOR
  160.     vbdObject_FillColor = m_FillColor
  161. End Property
  162. ' Set the object's FillColor.
  163. Public Property Let vbdObject_FillColor(ByVal new_value As OLE_COLOR)
  164.     m_FillColor = new_value
  165. End Property
  166.  
  167. ' Return the object's FillStyle.
  168. Public Property Get vbdObject_FillStyle() As FillStyleConstants
  169.     vbdObject_FillStyle = m_FillStyle
  170. End Property
  171. ' Set the object's FillStyle.
  172. Public Property Let vbdObject_FillStyle(ByVal new_value As FillStyleConstants)
  173.     m_FillStyle = new_value
  174. End Property
  175.  
  176. ' Draw the object on the canvas.
  177. Private Sub vbdObject_Draw(ByVal Canvas As Object)
  178. Dim obj As vbdObject
  179.  
  180.     For Each obj In SceneObjects
  181.         obj.Draw Canvas
  182.     Next obj
  183. End Sub
  184.  
  185. ' Set the objects' Selected statuses.
  186. Private Property Let vbdObject_Selected(ByVal RHS As Boolean)
  187. Dim obj As vbdObject
  188.  
  189.     For Each obj In SceneObjects
  190.         obj.Selected = RHS
  191.     Next obj
  192. End Property
  193. ' Return the objects' Selected status.
  194. Private Property Get vbdObject_Selected() As Boolean
  195. Dim obj As vbdObject
  196.  
  197.     If SceneObjects.Count = 0 Then
  198.         vbdObject_Selected = False
  199.     Else
  200.         Set obj = SceneObjects(1)
  201.         vbdObject_Selected = obj.Selected
  202.     End If
  203. End Property
  204.  
  205. ' Find the object at this position.
  206. Public Function FindObjectAt(ByVal X As Single, ByVal Y As Single) As vbdObject
  207. Dim obj As vbdObject
  208. Dim i As Integer
  209.  
  210.     Set FindObjectAt = Nothing
  211.  
  212.     ' Search for the object starting with
  213.     ' the objects on top.
  214.     For i = SceneObjects.Count To 1 Step -1
  215.         Set obj = SceneObjects(i)
  216.         If obj.IsAt(X, Y) Then
  217.             Set FindObjectAt = obj
  218.             Exit For
  219.         End If
  220.     Next i
  221. End Function
  222.  
  223. ' Return True if the object is at this location.
  224. Private Function vbdObject_IsAt(ByVal X As Single, ByVal Y As Single) As Boolean
  225.  
  226. End Function
  227.  
  228.  
  229. ' Initialize the object using a serialization string.
  230. ' The serialization does not include the
  231. ' ObjectType(...) part.
  232. Private Property Let vbdObject_Serialization(ByVal RHS As String)
  233. Dim obj As vbdObject
  234. Dim token_name As String
  235. Dim token_value As String
  236.  
  237.     ' Remove non-printable characters from the
  238.     ' serialization.
  239.     RHS = RemoveNonPrintables(RHS)
  240.  
  241.     ' Start with no objects.
  242.     Set SceneObjects = New Collection
  243.  
  244.     ' Read tokens until there are no more.
  245.     Do While Len(RHS) > 0
  246.         ' Read a token.
  247.         GetNamedToken RHS, token_name, token_value
  248.         Select Case token_name
  249.             Case "vbdLine"
  250.                 Set obj = New vbdLine
  251.             Case "vbdPolygon"
  252.                 Set obj = New vbdPolygon
  253.             Case "vbdScribble"
  254.                 Set obj = New vbdScribble
  255.             Case "vbdScene"
  256.                 Set obj = New vbdScene
  257.             Case Else
  258.                 Set obj = Nothing
  259.         End Select
  260.  
  261.         ' Initialize the object.
  262.         If Not obj Is Nothing Then
  263.             obj.Serialization = token_value
  264.             SceneObjects.Add obj
  265.             Set obj = Nothing
  266.         End If
  267.     Loop
  268. End Property
  269.  
  270. ' Return a serialization string for the object.
  271. Private Property Get vbdObject_Serialization() As String
  272. Dim txt As String
  273. Dim obj As vbdObject
  274.  
  275.     ' Don't bother with this object's
  276.     ' drawing properties.
  277.  
  278.  
  279.     ' Get the sub-objects' serializations.
  280.     For Each obj In SceneObjects
  281.         txt = txt & vbCrLf & "  " & _
  282.             obj.Serialization
  283.     Next obj
  284.  
  285.     vbdObject_Serialization = _
  286.         "vbdScene(" & txt & vbCrLf & _
  287.         "  )"
  288. End Property
  289.